package com.shuzi.MediaShare.view;

import android.R.integer;
import android.content.Context;
import android.graphics.Color;
import android.provider.MediaStore.Audio.Media;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.model.MediaType;

/**
 * 筛选布局
 * 
 * @author lung 上午12:10:57 2013
 */
public class TypeChoiceLayout extends RelativeLayout
{

	/** 类型 */
	private AutoLineFeed choiceTypeViewGroup;

	/** 时间 */
	private AutoLineFeed choiceTimeViewGroup;

	/** 地区 */
	private AutoLineFeed choiceAraeViewGroup;

	/** 选中颜色 */
	private int selectItemColor;

	/** 默认颜色 */
	private int defultItemColor;

	public String selectAreaItem = "全部地区";

	public String selectTypeItem = "全部类型";

	public String selectTimeItem = "不限";

	/** 取消按钮 */
	private Button cancelButton;

	/** 确定按钮 */
	public Button okButton;

	public TypeChoiceLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init(context);
	}

	public TypeChoiceLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	public TypeChoiceLayout(Context context)
	{
		super(context);
		init(context);
	}

	/**
	 * 初始化布局
	 */
	public void init(Context context)
	{
		addView(View.inflate(context, R.layout.type_choice_, null),
				new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT));

		choiceTypeViewGroup = (AutoLineFeed) findViewById(R.id.typeChoiceRadioGroup);
		choiceTimeViewGroup = (AutoLineFeed) findViewById(R.id.timeChoiceRadioGroup);
		choiceAraeViewGroup = (AutoLineFeed) findViewById(R.id.areaChoiceRadioGroup);

		cancelButton = (Button) findViewById(R.id.cancelChoice);
		okButton = (Button) findViewById(R.id.okChoice);

		selectItemColor = Color.parseColor("#00eaff");
		defultItemColor = Color.LTGRAY;

		for (int i = 0; i < Constant.times.length; i++)
		{
			TextView typeTextView = new TextView(context);
			typeTextView.setText(Constant.times[i]);
			typeTextView.setTextColor(defultItemColor);
			choiceTimeViewGroup.addView(typeTextView);
			if (i == 0)
			{
				typeTextView.setTextColor(selectItemColor);
			}
			typeTextView.setTag(Constant.times[i]);
			typeTextView.setOnClickListener(timesListener);

		}

		cancelButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				setVisibility(View.GONE);

			}
		});
	}

	/**
	 * 设置类型
	 * 
	 * @param context
	 * @param type
	 */
	public void setInfo(Context context, MediaType type)
	{
		TextView allTextView = new TextView(context);
		allTextView.setText("全部类型");
		allTextView.setTextColor(selectItemColor);
		choiceTypeViewGroup.addView(allTextView);
		allTextView.setOnClickListener(typesListener);
		allTextView.setTag("全部类型");
		
		TextView allTextView2 = new TextView(context);
		allTextView2.setText("全部地区");
		allTextView2.setTextColor(selectItemColor);
		choiceAraeViewGroup.addView(allTextView2);
		allTextView2.setOnClickListener(araeListener);
		allTextView2.setTag("全部地区");

		for (int i = 0; i < type.typeList.length; i++)
		{
			TextView typeTextView = new TextView(context);
			typeTextView.setText(type.typeList[i]);
			typeTextView.setTextColor(defultItemColor);
			choiceTypeViewGroup.addView(typeTextView);
			typeTextView.setTag(type.typeList[i]);
			typeTextView.setOnClickListener(typesListener);
		}
		for (int i = 0; i < type.areaList.length; i++)
		{
			TextView typeTextView = new TextView(context);
			typeTextView.setText(type.areaList[i]);
			typeTextView.setTextColor(defultItemColor);
			choiceAraeViewGroup.addView(typeTextView);
			typeTextView.setOnClickListener(araeListener);
			typeTextView.setTag(type.areaList[i]);
		}
	}

	private OnClickListener araeListener = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			TextView lasTextView = (TextView) findViewWithTag(selectAreaItem);
			lasTextView.setTextColor(defultItemColor);
			TextView textView = (TextView) findViewWithTag(v.getTag());
			textView.setTextColor(selectItemColor);
			selectAreaItem = (String) v.getTag();

		}
	};

	private OnClickListener typesListener = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			TextView lasTextView = (TextView) findViewWithTag(selectTypeItem);
			lasTextView.setTextColor(defultItemColor);
			TextView textView = (TextView) findViewWithTag(v.getTag());
			textView.setTextColor(selectItemColor);
			selectTypeItem = (String) v.getTag();

		}
	};

	private OnClickListener timesListener = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{
			TextView lasTextView = (TextView) findViewWithTag(selectTimeItem);
			lasTextView.setTextColor(defultItemColor);
			TextView textView = (TextView) findViewWithTag(v.getTag());
			textView.setTextColor(selectItemColor);
			selectTimeItem = (String) v.getTag();

		}
	};

	/**
	 * 获取索引
	 * 
	 * @param objects
	 * @param data
	 * @return
	 */
	public String getIndex(String[] objects, String data)
	{
		for (int i = 0; i < objects.length; i++)
		{
			if (data.equals(objects[i]))
			{
				int index = (int) Math.pow(2,i);
				return index + "";
			}
		}
		return null;
	}

}
